<?php
class post
{
	public function defaultaction(){
		lib::sendto();
	}

	public function quick()
	{
		lib::setitem('POST', $_POST);	// save current post
		$type = $_POST['type'];
		
		lib::setmessage(lang::FOR_DETAIL);
		lib::sendto('/post/'.$type);
	}
	
	public function found()
	{
		lib::setitem('title', '提交招领信息');
		echo view::show('post/found', lib::getitem('POST', lib::NO_PERSISTENT_STORAGE));
	}

	public function lost()
	{
		lib::setitem('title', '提交失物信息');
		echo view::show('post/lost', lib::getitem('POST', lib::NO_PERSISTENT_STORAGE));
	}


	public function process()
	{
		lib::setitem('POST', $_POST);	// save current post
		
		$type = $_POST['type'];
		
		if(!lib::checkseccode($_POST['seccode'])){
			lib::seterror(lang::WRONG_SECCODE);
			//lib::sendto('/post/'.$type);
		}
		
		$item = new item();
		
		switch($type){
			case 'lost':
				$item->type = 0;
				break;
			case 'found':
				$item->type = 1;
				break;
			default:
				lib::seterror(lang::WRONG_POST_TYPE);
				lib::sendto('/post/'.$type);
		}
		
		$user = lib::getitem('user');		
		$item->uid = $user->id;

		/* 临时用户 */
		if($_POST['usertype']=="0"){	
			$guest = new guest();
			$guest->title = htmlspecialchars($_POST['guest'].$_POST['title']);
			$guest->contact = htmlspecialchars($_POST['contact']);
			$guest->editcode = lib::makehashedpassword(NULL,$_POST['editcode']);
			$guest->available = date('Y-m-d', time() + $_POST['available']* 24 * 60 * 60);
			$guest->save();
				
			$item->gid = $guest->id;
		}
		
		/* 标签 */
		if($_POST['tag']){
			$word = htmlspecialchars(trim($_POST['tag']));
			$tag = new tag(array('word'=>$word));
			if(!$tag->id){
				$tag->word = $word;
				$tag->count = 1;
			}else{
				$tag->count = $tag->count+1;
			}
			$tag->save();
			$item->tag = $tag->id;
		}
		
		/* 上传文件 */
		if(isset($_FILES['imagefile'])&&($_FILES['imagefile']['error']==UPLOAD_ERR_OK)){

			if(1024*100<$_FILES['imagefile']['error']){	// 大于100Kb
				lib::seterror(lang::BIG_IMAGE);
				lib::sendto('/post/'.$type);
			}

			$thumbfile = 'uploads/' . uniqid(date('Ymd')."_") . ".jpg";
			$uploadfile = $_FILES['imagefile']['tmp_name'];
			$size = GetImageSize($uploadfile);
			
			if($size[2]==1)	// gif
				$im_in = imagecreatefromgif($uploadfile);
			else if($size[2]==2)	// jpg
				$im_in = imagecreatefromjpeg($uploadfile);
			else if($size[2]==3)	// png
				$im_in = imagecreatefrompng($uploadfile);
			else{
				lib::seterror(lang::WRONG_IMAGE_TYPE);
				lib::sendto('/post/'.$type);
			}
			
			$width = min($size[0], 400);
			$height = $size[1]*($width/$size[0]);
			$im_out = ImageCreateTrueColor($width, $height);
			ImageCopyresampled($im_out, $im_in, 0, 0, 0, 0, $width, $height, $size[0], $size[1]);
			Imagejpeg($im_out, $thumbfile);
			chmod($thumbfile, 0775);
			ImageDestroy($im_in);
			ImageDestroy($im_out);
			
			die($thumbfile);
			$item->imageurl = $thumbfile;
		}
		
		/* 地图数据 */
		if($_POST['usemap'])
			$item->mapdata = $_POST['mapdata'];
		
		$item->what = htmlspecialchars($_POST['what']);
		$item->when = htmlspecialchars($_POST['when']);
		$building = $_POST['building']=="null"?"":$_POST['building'];
		$item->where = htmlspecialchars($building."#".trim($_POST['room']));
		$item->detail = htmlspecialchars($_POST['detail']);
		
		$item->save();
		
		lib::sendto("/item/detail/".$item->id);
	}
}